package cn.cimoc.controller;

import cn.cimoc.pojo.Hero;
import cn.cimoc.service.HeroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
public class HeroController {

    @Autowired
    HeroService service;

    @RequestMapping("/")
    public String index() {
        return "/add";
    }

    @PostMapping("/insert")
    public String insert(ModelMap map,
                         @RequestParam("name")String name,
                         @RequestParam("type")String type,
                         @RequestParam("price")Long price) {

        return service.insert(map, name, type, price);
    }

    @PostMapping("/delete")
    public String delete(ModelMap map,
                         @RequestParam("id")Long id) {
        return service.delete(map, id);
    }

    @PostMapping("/mod")
    public String mod(ModelMap map,
                      @RequestParam("id")Long id,
                      @RequestParam("name")String name,
                      @RequestParam("type")String type,
                      @RequestParam("price")Long price) {
        Hero hero = new Hero(id, name, type, price);
        map.put("hero", hero);
        return "/update";
    }

    @PostMapping("/modify")
    public String modify(ModelMap map,
                      @RequestParam("id")Long id,
                      @RequestParam("name")String name,
                      @RequestParam("type")String type,
                      @RequestParam("price")Long price) {
        return service.modify(map, id, name, type, price);
    }

}
